#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Info:
	pass # end of class


import gtk
from gtkmvc import View

class MyView (View):

	glade = "window.glade"
	top = "window1"
	
	def __init__(self, ctrl):
		View.__init__(self, ctrl, self.glade)    
		return

	def getSearchValue(self):
		return self['txtSaisie'].get_text()	

	def displayMovie(self, info):
		self['txtSaisie'].set_text("")
		self['lblResultat'].set_text("1/%s\nid=%s\nTitre=%s" % (info.size, info.id, info.title))

	pass # end of class

import imdb
from gtkmvc import Model
class MyModel(Model):

	def __init__(self):
		Model.__init__(self)        
		return

	def getMovieInfos(self, searchValue):
		"""
		Recherche des informations sur le film
		Retourne un tableau d'info sur le film
		"""	
		# Create the object that will be used to access the IMDb's database.
		ia = imdb.IMDb() # by default access the web.

		# Search for a movie (get a list of Movie objects).
		info = Info()
		try:	
			s_result = ia.search_movie(searchValue)
			info.title = s_result[0]['long imdb canonical title']
			info.id = s_result[0].movieID
			info.size = len(s_result)
		except imdb.IMDbError, e:
			print "Probably you're not connected to Internet.  Complete error report:"
			print e
		return info

	pass # end of class



from gtkmvc import Controller
class MyController(Controller):

	def __init__(self, model):
		Controller.__init__(self, model)
		return

	def register_view(self, view):
		Controller.register_view(self, view)

	# signals handling
	def on_search_click(self, button):
		print 'search clicked'
		searchValue = self.view.getSearchValue()
		movieInfos = self.model.getMovieInfos(searchValue)	
		self.view.displayMovie(movieInfos)

	def on_window1_delete(self, w, e):
		print 'window1_delete'
		gtk.main_quit()
		return True

	def on_quit_clicked(self,button):
		gtk.main_quit()
		return True


	pass # end of class



m = MyModel()
c = MyController(m)
v = MyView(c)

gtk.main()
